import api from '~api/index'
import util from '~src/lib/util'
const state = {
  openid: util.localItem.get('openid') || null,
  auth:{},
  shareInfo:{}
}
const mutations = {
  ['setOpenid'] (state, data) {
    util.localItem.set('openid',data.open_id)
    state.openid = data.open_id
  },
  ['setAuth'] (state, data) {
    state.auth = data
  },
  ['setShareInfo'] (state, data) {
    state.shareInfo = data
  }
}
const actions = {
  async ['getOpenid'] ({commit},wxcode) {
    const { data: { data, code } } = await api.get('/v1/getOpenId',{code: wxcode})
    if (data && code === '200') {
      commit('setOpenid', data)
      if (data.open_id) {
        location.href = '/vip'
      }
    }
    // commit('setOpenid', 'owlVEw0WQ87nGhhq4b9-f1V43JOw')  测试用的openid
    // commit('setOpenid', 'owlVEwzZKyMcHmfwNUtKoDadc_l8')  测试用的openid
  },
  async ['getWxSdk'] ({commit},url) {
    const { data: { data,code } } = await api.post('/v1/getSignPackage', {openid: util.localItem.get('openid'),url:url})
    if (data && code === '200') {
      commit('setAuth', data)
    }
  },
  async ['getShareInfo'] ({commit},gsId) {
    const { data: { data,code } } = await api.post('/v1/getShareInfo', {gsId:gsId})
    if (data && code === '200') {
      commit('setShareInfo', data)
    }
  },
  async ['getShareCoupon'] ({commit},config) {
    const { data: { data, code , msg } } = await api.post('/v1/sendShareCoupon', {openid: util.localItem.get('openid'),gsId:config.gsId,orderNo:config.orderNo})
    if (data && code === '200') {
      commit('TOAST_SHOW', {content: '领取成功', msg: true})
    } else {
      commit('TOAST_SHOW', {content: msg, msg: true})
    }
  }
}
const getters = {
  ['getOpenid'] (state) {
    return state.openid
  },
  ['getiframeSrc'] (state) {
    return state.iframeSrc
  },
  ['getWxSdk'] (state) {
    return state.auth
  },
  ['getShareInfo'] (state) {
    return state.shareInfo
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
